import time
from valkka.core import *
glthread        =OpenGLThread ("glthread")
gl_in_filter    =glthread.getFrameFilter()

avthread        =AVThread("avthread",gl_in_filter)
av_in_filter    =avthread.getFrameFilter()

"""<rtf>
USBDeviceThread reads and multiplexes all USB cameras
<rtf>"""
usbthread       =USBDeviceThread("usbthread")

"""<rtf>
Define the usb camera (/dev/video2) and where it is going to be streamed (to av_in_filter with slot number 1):
<rtf>"""
ctx = USBCameraConnectionContext("/dev/video2", 1, av_in_filter)
# The default resolution is 720p 
# If you want to set the width and height yourself, uncomment the following line
# ctx.width  = 1920; ctx.height = 1080;
  
glthread.startCall()
avthread.startCall()
usbthread.startCall()

avthread.decodingOnCall()

window_id =glthread.createWindow()

glthread.newRenderGroupCall(window_id)

context_id=glthread.newRenderContextCall(1,window_id,0) # slot, render group, z

time.sleep(1)

usbthread.playCameraStreamCall(ctx);

"""<rtf>
Stream for a minute.  Patience.  At least the HD Pro Webcam C920 does not send keyframes too often ..
<rtf>"""
time.sleep(60)

usbthread.stopCameraStreamCall(ctx);

glthread.delRenderContextCall(context_id)
glthread.delRenderGroupCall(window_id)

avthread.decodingOffCall()

usbthread.stopCall()
avthread.stopCall()
glthread.stopCall()

print("bye")
